<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程订单回收站
 */
class Ordrecycle extends Backend
{
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = 'order.id,subject.title,user.nickname,code,total';

    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->OrderModel = model('Subject.Order');
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->OrderModel
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->OrderModel
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            // echo $this->SubjectOrderModel->getlastSql();
            // exit;
            $result = array("total" => $total, "rows" => $list);
            // var_dump($list);
            return json($result);
        }
        return $this->view->fetch();
    }

    // 真正删除
    public function del($ids=null)
    {
        $id=empty($ids) ? [] : explode(',',$ids);

        foreach($id as $item)
        {
            $res=$this->OrderModel->onlyTrashed()->find($item);
            if(!$res)
            {
                $this->error('未找到当前订单');
                exit;
            }
        }
        $result=$this->OrderModel->destroy($id,true);
        if($result)
        {
            $this->success('删除订单成功');
            exit;
        }else{
            $this->error($this->OrderModel->getError());
            exit;
        }
    }

    // 还原
    public function reduction($ids=null)
    {
        $id=empty($ids) ? [] : explode(',',$ids);
        $data=[];
        foreach($id as $item)
        {
            $res=$this->OrderModel->onlyTrashed()->find($item);
            if(!$res)
            {
                $this->error('未找到当前订单');
                exit;
            }
            $list=['id'=>$item,'deletetime'=>null];
            array_push($data,$list);
        }

        // 批量还原
        $result=$this->OrderModel->isUpdate(true)->saveAll($data);
        if($result)
        {
            $this->success('还原订单成功');
            exit;
        }else
        {
            $this->error($this->OrderModel->getError());
            exit;
        }
    }

}